﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;

public partial class Example6 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Label1.Text = "0";
            LoadRecord(Convert.ToInt32(Label1.Text));
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //string filePath = Server.MapPath("newbooks2.xml");
        //XmlDocument doc = new XmlDocument();
        //doc.LoadXml("<?xml version='1.0' ?><bookstore></bookstore>");

        //XmlNode parentNode = doc.CreateElement("book");
        ////doc.DocumentElement.AppendChild(node1);

        //XmlAttribute attribute = doc.CreateAttribute("type");
        //attribute.InnerText = "novel";
        //parentNode.Attributes.Append(attribute);

        //XmlNode currentNode = doc.CreateElement("title");
        //currentNode.InnerText = "Harry Potter";


        //parentNode.AppendChild(currentNode);
        //doc.DocumentElement.AppendChild(parentNode);


        //doc.Save(filePath);

        string filePath = Server.MapPath("newbooks2.xml");
        XmlDocument doc = new XmlDocument();
        doc.Load(filePath);

        XmlNode parentNode = doc.CreateElement("book");
        //doc.DocumentElement.AppendChild(node1);

        XmlAttribute attribute = doc.CreateAttribute("type");
        attribute.InnerText = TextBox2.Text;
        parentNode.Attributes.Append(attribute);

        XmlNode currentNode = doc.CreateElement("title");
        currentNode.InnerText = TextBox1.Text;


        parentNode.AppendChild(currentNode);
        doc.DocumentElement.AppendChild(parentNode);


        doc.Save(filePath);
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Label1.Text = (Convert.ToInt32(Label1.Text) - 1).ToString();
        LoadRecord(Convert.ToInt32(Label1.Text));
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        Label1.Text = (Convert.ToInt32(Label1.Text) + 1).ToString();
        LoadRecord(Convert.ToInt32(Label1.Text));
    }

    private void LoadRecord(int index)
    {
        int recordCount = 0;

        try
        {
            string filePath = Server.MapPath("newbooks2.xml");
            XmlDocument doc = new XmlDocument();
            doc.Load(filePath);

            XmlNodeList nodeList = doc.GetElementsByTagName("book");
            recordCount = nodeList.Count;

            if (index == 0)
            {
                Button2.Enabled = false;
            }
            else
            {
                Button2.Enabled = true;
            }

            if (index == recordCount - 1)
            {
                Button3.Enabled = false;
            }
            else
            {
                Button3.Enabled = true;
            }


            XmlNode currentNode = nodeList[index];
            string bookType = currentNode.Attributes["type"].InnerText;
            TextBox2.Text = bookType;

            nodeList = doc.GetElementsByTagName("title");
            currentNode = nodeList[index];
            TextBox1.Text = currentNode.InnerText;
        }
        catch (Exception)
        {
            throw;
        }
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        UpdateRecord(Convert.ToInt32(Label1.Text));
    }

    private void UpdateRecord(int index)
    {
        string filePath = Server.MapPath("newbooks2.xml");
        XmlDocument doc = new XmlDocument();
        doc.Load(filePath);

        XmlNodeList nodeList = doc.GetElementsByTagName("book");


        XmlNode currentNode = nodeList[index];
        currentNode.Attributes["type"].InnerText = TextBox2.Text;


        nodeList = doc.GetElementsByTagName("title");
        currentNode = nodeList[index];
        currentNode.InnerText = TextBox1.Text;

        doc.Save(filePath);
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
        DeleteRecord(Convert.ToInt32(Label1.Text));
        LoadRecord(0);
    }

    private void DeleteRecord(int index)
    {
        string filePath = Server.MapPath("newbooks2.xml");
        XmlDocument doc = new XmlDocument();
        doc.Load(filePath);

        XmlNodeList nodeList = doc.GetElementsByTagName("book");


        XmlNode currentNode = nodeList[index];
        doc.DocumentElement.RemoveChild(currentNode);

        doc.Save(filePath);
    }
}